package com.unibert.podcastq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class Comentar extends Activity {

	private Integer id;
	private ImageButton Coment;
	private EditText nombre;
	private EditText comentario;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comentar);
		nombre = (EditText) findViewById(R.id.nombre);
		comentario = (EditText) findViewById(R.id.comentario);

		Bundle extras = getIntent().getExtras();
		id=Integer.parseInt(extras.getString("id"));
		Coment = (ImageButton) findViewById(R.id.Comment);
		Coment.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(comentario.getText().length()>0&&nombre.getText().length()>0){
					new AsyncTaskUpdateUrlcomments2().execute();
				}

			}
		});


	}

	private class AsyncTaskUpdateUrlcomments2 extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				URI comentar = new URI( "http",
						"cristianosuniversitarios.org",
						"",
						"wpapi=comment&" +
						"dev=1&" +
						"name="+nombre.getText()+"&" +
						"email=Qcristianosuniversitarios@gmail.com&" +
						"content="+comentario.getText()+"&" +
						"post_id="+id,null);
				URL comentarBueno = new URL(comentar.toASCIIString());
				InputStream input = comentarBueno.openStream();
				BufferedReader reader = new BufferedReader(new InputStreamReader(input,"iso-8859-1"),8);
				String line = null;
				while ((line = reader.readLine()) != null) {
				}
				input.close();

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;

		}
		protected void onPostExecute(final String result) {
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(Comentar.this, Comentar.this.getString(R.string.Comentado), duration);
			toast.show();
			Comentar.this.onBackPressed();
		}






	}
}

